Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scoring: reduce allocations for addScore #670

Merged
merged 2 commits into from
Oct 23, 2023
Merged

Conversation

stefanhengl
Copy link
Member

Follow-up from this comment.

We add a bit more complexity to addScore but avoid allocating a string if debugScore = false.

Test plan:

  • I ran a couple of benchmarks and confirmed we don't allocate.

@stefanhengl stefanhengl requested a review from a team October 23, 2023 10:12
Copy link
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative feedback inline.

While you are cleaning this up, its probably worth adding a closure in the main place we call addScore to seach call site doesn't need to also pass in debugScore.

eval.go Outdated
Comment on lines 39 to 50
func (m *FileMatch) addScore(what string, computed float64, raw float64, debugScore bool) {
if computed != 0 && debugScore {
m.Debug += fmt.Sprintf("%s:%.2f", what, computed)
if raw != -1 {
m.Debug += fmt.Sprintf("(%s), ", strconv.FormatFloat(raw, 'f', -1, 64)) // uses the smallest number of digits necessary to represent raw.
} else {
m.Debug += ", "
}
}
m.Score += s
m.Score += computed
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternative implementation to still aid reading stuff at the call sites.

func addScore(s float64, debugScore bool, whatFmt string, whatArgs ...any) {
  if s != 0 && debugScore {
    var b strings.Builder
    fmt.Fprintf(&b, whatFmt, whatArgs...)
    fmt.Fprintf(&b, ":%.2f, ", s)
    m.Debug += b.String()
  }
  m.Score += s
}

What I don't like about what I propose is it was nice to have the string first. Then maybe consider something like

func addScore(what string, s float64, debugScore bool, whatArgs ...any) {
  if s != 0 && debugScore {
    var b strings.Builder
    b.WriteString(what)
    for _, v := range whatArgs {
      fmt.Fprintf(&b, ":%v", v)
    }
    fmt.Fprintf(&b, ":%.2f, ", s)
    m.Debug += b.String()
  }
  m.Score += s
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...any will still cause an alloc. I added the closure to the call sites and updated the code to use strings.Builder because it does read nicer.

@github-actions
Copy link
Contributor

Fuzz test failed on commit 51eae7c. To troubleshoot locally, use the GitHub CLI to download the seed corpus with

gh run download 6612571517 -n testdata

we add a bit more complexity to addScore but avoid allocation a string
if debugScore = false.

Test plan:
- I ran a couple of benchmarks and checked the allocations.
@stefanhengl stefanhengl merged commit 0a17ccb into main Oct 23, 2023
8 checks passed
@stefanhengl stefanhengl deleted the sh/reduce-allocs branch October 23, 2023 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants